package com.gszcn.tools.table;

import com.gszcn.tools.table.metadata.SqlMetadata;

import javax.annotation.processing.*;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import java.util.HashSet;
import java.util.Set;

/**
 * 获取类上面的注解
 */
@SupportedAnnotationTypes({"*"})
public class AnnotationProcessor extends AbstractProcessor {
    /**
     * 实体表注解
     */
    public static final String ENTITY_ANNOTATION = "javax.persistence.Entity";
    /**
     * table
     */
    public static final String TABLE_ANNOTATION = "javax.persistence.Table";
    /**
     * 环境数据初始化
     */
    private static SqlMetadata sqlMetadata;
    /**
     * 处理注解方法
     * @param annotations
     * @param env
     * @return
     */
    public boolean process(Set <? extends TypeElement> annotations, RoundEnvironment env) {
        /**
         * 初始化完成关闭修改
         */
        if(!sqlMetadata.EntityTable.isEmpty()){
            return false;
        }
        System.out.println("java12345");
        sqlMetadata.setProcess(annotations,env);

        return true;
    }



    @Override
    public Set <String> getSupportedAnnotationTypes() {
        return new HashSet <String>(){{
            add(ENTITY_ANNOTATION);
            add(TABLE_ANNOTATION);
        }};
    }

    /**
     * 初始化环境
     * @param processingEnv
     */
    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        sqlMetadata=new SqlMetadata(processingEnv);
    }

    @Override
    public Iterable <? extends Completion> getCompletions(Element element, AnnotationMirror annotation, ExecutableElement member, String userText) {
        Iterable <? extends Completion> completions = super.getCompletions(element, annotation, member, userText);
        return completions;
    }

}
